<template>
  <div>
    <!-- 面包屑指引 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索  clearable 是否可清空 @clear清空事件-->
    <el-input
      placeholder="请输入内容"
      v-model="userfoin.query"
      clearable
      @clear="uesrsdata"
    >
      <el-button
        slot="append"
        icon="el-icon-search"
        @click="uesrsdata"
      ></el-button>
    </el-input>

    <el-button type="warning" id="addsss" @click="dialogFormVisible = true"
      >添加商品</el-button
    >
    <!-- 渲染列表 -->
    <el-table :data="superusers" border style="width: 100%">
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column prop="goods_name" label="商品名称" width="680">
      </el-table-column>
      <el-table-column prop="goods_price" label="价格"> </el-table-column>
      <el-table-column prop="goods_number" label="数量"> </el-table-column>
      <el-table-column prop="goods_weight" label="重量"> </el-table-column>
      <!-- 利用时间戳格式化，获取当前下单时间 -->
      <el-table-column prop="add_time" label="添加时间" width="200">
        <!-- 格式化时间 -->
        <template slot-scope="scope">
          {{ scope.row.add_time | newFromTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- 查找商品 -->
          <el-button
            icon="el-icon-search"
            circle
            size="mini"
            @click="giveshop(scope.row)"
          ></el-button>
          <!-- 编辑商品 -->
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            size="mini"
            @click="givesshop(scope.row)"
          ></el-button>
          <!-- 删除商品 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            size="mini"
            @click="outshop(scope.row.goods_id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页的数据 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="userfoin.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- //添加商品弹出框 -->
    <el-dialog title="添加商品" :visible.sync="dialogFormVisible">
      <el-form :model="givssadd">
        <el-form-item label="商品名称">
          <el-input v-model="givssadd.goods_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="三级分类">
          <el-input v-model="givssadd.goods_cat" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input
            v-model="givssadd.goods_price"
            autocomplete="off"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input
            v-model="givssadd.goods_number"
            autocomplete="off"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="重量">
          <el-input
            v-model="givssadd.goods_weight"
            autocomplete="off"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="添加时间">
          <el-input v-model="givssadd.add_time" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="givesize">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 查找商品弹出框 -->
    <el-dialog title="查找商品" :visible.sync="dialogFormgiveVisible">
      <el-form :model="giveshppen">
        <el-form-item label="商品名称">
          <el-input
            v-model="giveshppen.goods_name"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input
            v-model="giveshppen.goods_price"
            autocomplete="off"
            type="number"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input
            v-model="giveshppen.goods_number"
            autocomplete="off"
            type="number"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="重量">
          <el-input
            v-model="giveshppen.goods_weight"
            autocomplete="off"
            type="number"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="添加时间">
          <el-input
            v-model="giveshppen.add_time"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 编辑商品弹出框 -->
    <el-dialog title="编辑商品" :visible.sync="dialogFormeideVisible">
      <el-form :model="giveside">
        <el-form-item label="商品名称">
          <el-input v-model="giveside.goods_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input
            v-model="giveside.goods_price"
            autocomplete="off"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input
            v-model="giveside.goods_number"
            autocomplete="off"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="重量">
          <el-input
            v-model="giveside.goods_weight"
            autocomplete="off"
            type="number"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormeideVisible = false">取 消</el-button>
        <el-button type="primary" @click="giveeled">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //获取到的数据
      superusers: [],
      userfoin: {
        query: "", //查询参数
        pagenum: 1, //页数
        pagesize: 10, //条数
      },
      total: 0, //总共商品条数
      //添加
      dialogFormVisible: false, //关闭弹出框
      givssadd: {
        goods_name: "", //商品名称
        goods_cat: "",
        goods_price: "", //价格
        goods_number: "", //数量
        goods_weight: "", //重量
      },
      //查找用户信息
      dialogFormgiveVisible: false,
      giveshppen: {},
      //编辑用户信息
      dialogFormeideVisible: false,
      giveside: {},
    };
  },
  methods: {
    // 删除商品
    outshop(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        //发起请求
        let { data } = await this.$req.delete(`goods/${id}`);
        console.log(data);
        if (data.meta.status == 200) {
          this.$message({
            type: "success", //弹窗类型
            message: data.meta.msg, //弹窗内容
          });
          //调用公用方法自动刷新
          this.uesrsdata();
        } else {
          this.$message.error(data.meta.msg);
        }
      });
    },
    //  编辑商品获取数据
    async givesshop(userinfo) {
      this.dialogFormeideVisible = true;
      this.giveside = userinfo;
    },
    // 点击确定编辑商品
    async giveeled() {
      //发送get请求获取最新数据
      var { data } = await this.$req.get("goods/" + this.giveside.goods_id);
      console.log(data);
      //把后端的数据和前端的数据进行合并，这样的话就前端也改后端也改
      this.giveside = Object.assign(data.data, this.giveside);

      //发起修改请求，根据get请求过来的数据以id查询出来之后，
      //根据双向绑定: 来去更改服务器的值。
      var { data } = await this.$req.put(
        "goods/" + this.giveside.goods_id,
        this.giveside
      );
      console.log(data);
      if (data.meta.status == 200) {
        this.$message({
          type: "success",
          message: data.meta.msg,
        });
        this.dialogFormeideVisible = false;
        this.uesrsdata();
      } else {
        this.$message.error(data.meta.msg);
      }
    },

    // 查找商品
    giveshop(userinfo) {
      this.dialogFormgiveVisible = true;
      this.giveshppen = userinfo;
    },

    //获取添加数据
    async givesize() {
      console.log(this.givssadd);
      let { data } = await this.$req.post("/goods", this.givssadd);
      console.log(data);
      if (data.meta.status == 201) {
        this.$message({
          message: data.meta.msg,
          type: "success",
        });
        this.dialogFormVisible = false;
        this.uesrsdata();
      } else {
        this.$message.error(data.meta.msg);
      }
    },
    //设置跳转到第几页的页数,调用公有方法
    handleCurrentChange(pagenum) {
      this.userfoin.pagenum = pagenum;
      this.uesrsdata();
    },

    // 设置每页的条数，调用公有方法
    handleSizeChange(pagesize) {
      this.userfoin.pagesize = pagesize;
      this.uesrsdata();
    },

    //公用方法，获取数据的，页数和条数
    async uesrsdata() {
      let { data } = await this.$req.get("/goods", { params: this.userfoin });
      console.log(data.data);
      if (data.meta.status == 200) {
        this.$message({
          message: data.meta.msg,
          type: "success",
        });
        //然后把数据赋值给superusers
        this.superusers = data.data.goods;
        this.total = data.data.total;
      } else {
        this.$message.error(data.meta.msg);
      }
    },
  },

  //页面加载数据，(页面加载跳转的条数数据)渲染页面，调用上面的方法
  mounted() {
    this.uesrsdata();
  },
};
</script>

<style>
#addsss {
  position: absolute;
  top: 210px;
  left: 50%;
}
</style>